package com.centit.flowNode.dao;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONArray;
import com.centit.flowNode.po.FlowNode;
import com.centit.framework.core.dao.CodeBook;
import com.centit.framework.jdbc.dao.BaseDaoImpl;
import com.centit.framework.jdbc.dao.DatabaseOptUtils;
import com.centit.support.database.utils.QueryAndNamedParams;
import com.centit.support.database.utils.QueryUtils;
@Repository
public class FlowNodeDao extends BaseDaoImpl<FlowNode,java.lang.String>{

	public static final Log log = LogFactory.getLog(FlowNodeDao.class);
	
	@Override
	public Map<String, String> getFilterField() {
		if( filterField == null){
			filterField = new HashMap<String, String>();

			filterField.put("recId" , CodeBook.EQUAL_HQL_ID);
			
			filterField.put("flowRecId", CodeBook.EQUAL_HQL_ID);
			
			filterField.put("status", CodeBook.EQUAL_HQL_ID);
			
			filterField.put("nodeNum", CodeBook.EQUAL_HQL_ID);
			
			filterField.put("ideaRecId", CodeBook.EQUAL_HQL_ID);
			
			
		}
		return filterField;
	} 
	
	public void updateAllFlowNodeEnd(String flowRecId){
		String sql="update T_FLOW_NODE set status='C' where FLOW_REC_ID=? ";
		DatabaseOptUtils.doExecuteSql(this, sql, new Object[] {flowRecId});
	}
	public void updateFlowNodeStatus(String flowRecId,String userCode){
		String sql="update T_FLOW_NODE set status='C' where FLOW_REC_ID=? and next_user_code=? and status='N' ";
		DatabaseOptUtils.doExecuteSql(this, sql, new Object[] {flowRecId,userCode});
	}
	public Integer getMaxNum(String flowRecId) {
		String str="select TO_NUMBER(max(NODE_NUM)) num from T_FLOW_NODE where FLOW_REC_ID=?";
		Object o;
		try {
			o = DatabaseOptUtils.getScalarObjectQuery(this, str, flowRecId);
			if(o!=null) {
				BigDecimal bigDecimal= (BigDecimal)o;
				return Integer.parseInt(bigDecimal.toString());
			}
		} catch (SQLException e) {
			System.out.println("获取flowNum最大值出错（flowRecId："+flowRecId+"）");
			return null;
		} catch (IOException e) {
			System.out.println("获取flowNum最大值出错（flowRecId："+flowRecId+"）");
			return null;
		}
		return null;
	}
	
	public void updateLastFlowNode(String flowRecId,String nodeNum,String userCode){
		String sql="update T_FLOW_NODE set next_user_code=? where FLOW_REC_ID=? and NODE_NUM=?";
		DatabaseOptUtils.doExecuteSql(this, sql, new Object[] {userCode,flowRecId,nodeNum});
	}
	
	public JSONArray listFlowUserCodeByFlowRecId(Map<String,Object> map){
		String sql="select * from T_IDEA_INFO i left join T_FLOW_NODE f on i.FLOW_REC_ID=f.FLOW_REC_ID and i.REC_ID=F.IDEA_REC_ID "
				+ " where f.status='C'  and f.data_valid='T' "
				+ "[ :flowRecId| and f.FLOW_REC_ID = :flowRecId] ";
		 QueryAndNamedParams queryAndNamedParams = QueryUtils.translateQuery(sql, map);
	        JSONArray json = DatabaseOptUtils.listObjectsByNamedSqlAsJson(this,
	                queryAndNamedParams.getQuery(), queryAndNamedParams.getParams(), null);
	        return json;
	}
}
